package com.manong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.manong.dao.UserMapper;
import com.manong.entity.Department;
import com.manong.dao.DepartmentMapper;
import com.manong.entity.User;
import com.manong.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manong.utils.DepartmentTree;
import com.manong.vo.query.DepartmentQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Dorr
 * @since 2025-03-03
 */
@Transactional
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Resource
    private UserMapper userMapper;
    /**
     * 查询部门列表
     * @param departmentQueryVo
     * @return
     */
    @Override
    public List<Department> findDepartmentList(DepartmentQueryVo departmentQueryVo) {
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
            //部门名称
            //第一个判断条件,若为ture,则表示此句有效,若为false,则此句无效
        queryWrapper.like(!ObjectUtils.isEmpty(departmentQueryVo.getDepartmentName()), "department_name", departmentQueryVo.getDepartmentName());
            //排序
        queryWrapper.orderByAsc("order_num");
        List<Department> departmentList = baseMapper.selectList(queryWrapper);

        if(!ObjectUtils.isEmpty(departmentQueryVo.getDepartmentName())){
            for (int i = 0; i < departmentList.size(); i++) {
                QueryWrapper<Department> queryWrapper1 = new QueryWrapper<Department>();
                queryWrapper1.eq("pid",departmentList.get(i).getId());
                List<Department> departments = baseMapper.selectList(queryWrapper1);
                for (int i1 = 0; i1 < departments.size(); i1++) {
                    departmentList.add(departments.get(i1));
                }
            }
        }
        Set<Department> dept = new LinkedHashSet<>();
        for (int i = 0; i < departmentList.size(); i++) {
            dept.add(departmentList.get(i));
        }
        departmentList = new ArrayList<>(dept);
        List<Department> departmentTree = new ArrayList<>();
        if (!departmentList.isEmpty()) { //如果list不为空,即没有条件或者条件正确
            //departmentList.stream().....getAsLong() 返回集合中最小的Pid
            departmentTree = DepartmentTree.makeDepartmentTree(departmentList, departmentList.stream().mapToLong(Department::getPid).min().getAsLong());
        }//如果list为空,即条件错误或无数据的时候,直接返回一个空集合
        //生成部门树
        return departmentTree;
    }

    /**
     * 查询上级部门列表
     * @return
     */
    @Override
    public List<Department> findParentDepartment() {
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        //排序
        queryWrapper.orderByAsc("order_num");
        //查询部门列表
        List<Department> departmentList = baseMapper.selectList(queryWrapper);
        //创建部门对象
        Department department = new Department();
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setPid(-1L);
        departmentList.add(department);
        //生成部门树列表
        List<Department> departmentTree = DepartmentTree.makeDepartmentTree(departmentList, -1L);
        //返回部门列表
        return departmentTree;
    }

    /**
     * 判断部门下是否有用户
     * @param id
     * @return
     */
    @Override
    public boolean hasUserOfDepartment(Long id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("department_id",id);
        if(userMapper.selectCount(queryWrapper)>0){
            return true;
        }
        return false;
    }

    /**
     * 判断部门下是否有子部门
     * @param id
     * @return
     */
    @Override
    public boolean hasChildrenOfDepartment(Long id) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        queryWrapper.eq("pid",id);
        if(baseMapper.selectCount(queryWrapper)>0){
            return true;
        }
        return false;
    }
}
